package com.net.test.controller;

import java.util.List;
import java.util.Locale;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.net.test.service.BoardService;
import com.net.test.vo.BoardVO;
import com.net.test.vo.FileVO;
import com.net.test.vo.TestVO;


/**
 * Handles requests for the application home page.
 */
@Controller
public class BoardController {
	
	private static final Logger logger = LoggerFactory.getLogger(BoardService.class);
	
	@Autowired
	BoardService boardService;
	
	/**
	 * Simply selects the home view to render by returning its name.
	 * @throws Exception 
	 */
	
	@RequestMapping(value = "/boardView.ncb", method = {RequestMethod.GET,RequestMethod.POST})
    public String boardView(Locale locale, Model model, HttpServletRequest request) throws Exception {
        logger.info("Welcome home! The client locale is {}.", locale);  
        
        String title = request.getParameter("title");
        
        /*List<BoardVO> view = (List<BoardVO>)boardService.viewData("testMapper.selectBoardView",title);*/
        BoardVO view = (BoardVO)boardService.viewData("testMapper.selectBoardView",title);
        
        model.addAttribute("view", view);
        
        
        return "boardView";
    }
	
	@RequestMapping(value = "/boardEdit.ncb", method = {RequestMethod.GET,RequestMethod.POST})
    public String boardEdit(Locale locale, Model model, HttpServletRequest request) throws Exception {
        logger.info("Welcome home! The client locale is {}.", locale);  
        
        String edit1 = request.getParameter("edit1");
        String edit2 = request.getParameter("edit2");
        
        BoardVO view = new BoardVO();
        view.setBoard_title(edit1);
        view.setBoard_contents(edit2);
        model.addAttribute("edit", view);
        
        
        return "boardEdit";
    }
	
	@RequestMapping(value = "/boardList.ncb", method = {RequestMethod.GET,RequestMethod.POST})
	public String boardList(Locale locale, Model model) throws Exception {
		logger.info("Welcome home! The client locale is {}.", locale);	
		
		List<BoardVO> list = (List<BoardVO>)boardService.selectData("testMapper.selectBoard");
		
        model.addAttribute("list", list);
		
		return "boardList";
	}
	
	@RequestMapping(value = "/boardWrite.ncb", method = {RequestMethod.GET,RequestMethod.POST})
	public String boardWrite(Locale locale, Model model) throws Exception {
		
		return "boardWrite";
		
	}
	
	@RequestMapping(value = "/boardInsert.ncb", method = {RequestMethod.GET,RequestMethod.POST})
	public String boardInsert(Locale locale, Model model, HttpServletRequest request) throws Exception {
		logger.info("Welcome home! The client locale is {}.", locale);
		
		BoardVO boardVO = new BoardVO();
		//FileVO  fileVO  = new FileVO();
		
		String title = request.getParameter("title");
		
		String contents = request.getParameter("contents");
		String boardNm = "게시판";
		//String fNm = request.getParameter("fileNmtxt");
		
		
		boardVO.setBoard_title(title);
		boardVO.setBoard_contents(contents);
		boardVO.setBoard_name(boardNm);
		
		int result = boardService.insertData("testMapper.addBoard", boardVO);
		//model.addAttribute("result", result );
		
		List<BoardVO> list = (List<BoardVO>)boardService.selectData("testMapper.selectBoard");
        model.addAttribute("list", list);
		
		return "boardList";
	}
	
	@RequestMapping(value = "/boardUpdate.ncb", method = {RequestMethod.GET,RequestMethod.POST})
    public String boardUpdate(Locale locale, Model model, HttpServletRequest request) throws Exception {
        logger.info("Welcome home! The client locale is {}.", locale);
        
        String pretitle = request.getParameter("pretitle");
        
        BoardVO boardVO = new BoardVO();
        //FileVO  fileVO  = new FileVO();
        
        String title = request.getParameter("title");
        
        String contents = request.getParameter("contents");
        
        //String fNm = request.getParameter("fileNmtxt");
        
        boardVO.setBoard_title(title);
        boardVO.setBoard_contents(contents);
        boardVO.setBoard_name(pretitle);
        
        int result = boardService.updateData("testMapper.updateBoard", boardVO);
        //model.addAttribute("result", result );
        
        List<BoardVO> list = (List<BoardVO>)boardService.selectData("testMapper.selectBoard");
        
        model.addAttribute("list", list);
        
        return "boardList";
    }
	
	@RequestMapping(value = "/boardDelete.ncb", method = {RequestMethod.GET,RequestMethod.POST})
    public String boardDelete(Locale locale, Model model, HttpServletRequest request) throws Exception {
        logger.info("Welcome home! The client locale is {}.", locale);
        
        String delTitle = request.getParameter("del");
        
        int result = boardService.deleteData("testMapper.deleteBoard", delTitle);
        //model.addAttribute("result", result );
        
        List<BoardVO> list = (List<BoardVO>)boardService.selectData("testMapper.selectBoard");
        model.addAttribute("list", list);
        
        return "boardList";
    }
	
	
}
